From c3bc9c804ec84292f7a851ddbfba92f3c544e3ac Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Fri, 5 Nov 2021 11:17:45 -0600 Subject: [PATCH] serial port names (#755) * try qserialportinfo again. * update docker images with qt serial port. * enhance windows device text to match device manager on windows. --- {gui => deprecated/gui}/serial_mac.cc | 0 gui/CMakeLists.txt | 23 ++----- gui/app.pro | 14 +--- gui/mainwindow.cc | 10 ++- gui/serial_unix.cc | 93 ++------------------------- gui/serial_win.cc | 53 +++------------ tools/Dockerfile_f32 | 2 +- tools/Dockerfile_f33 | 2 +- tools/Dockerfile_f34 | 2 +- tools/Dockerfile_focal | 1 + 10 files changed, 34 insertions(+), 166 deletions(-) rename {gui => deprecated/gui}/serial_mac.cc (100%) diff --git a/gui/serial_mac.cc b/deprecated/gui/serial_mac.cc similarity index 100% rename from gui/serial_mac.cc rename to deprecated/gui/serial_mac.cc diff --git a/gui/CMakeLists.txt b/gui/CMakeLists.txt index 3d0c77b0e..d7515a067 100644 --- a/gui/CMakeLists.txt +++ b/gui/CMakeLists.txt @@ -21,8 +21,8 @@ set(CMAKE_AUTORCC ON) # Find the QtCore library find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) -find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Gui Network Widgets Xml REQUIRED) -list(APPEND QT_LIBRARIES Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Gui Qt${QT_VERSION_MAJOR}::Network Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Xml) +find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Gui Network SerialPort Widgets Xml REQUIRED) +list(APPEND QT_LIBRARIES Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Gui Qt${QT_VERSION_MAJOR}::Network Qt${QT_VERSION_MAJOR}::SerialPort Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Xml) if(${Qt${QT_VERSION_MAJOR}Core_VERSION} VERSION_LESS 5.12) message(FATAL_ERROR "Qt version ${Qt${QT_VERSION_MAJOR}Core_VERSION} found, but version 5.12 or newer is required.") else() @@ -32,19 +32,6 @@ endif() find_package(Qt${QT_VERSION_MAJOR} COMPONENTS WebEngineWidgets WebChannel REQUIRED) list(APPEND QT_LIBRARIES Qt${QT_VERSION_MAJOR}::WebEngineWidgets Qt${QT_VERSION_MAJOR}::WebChannel) -if(APPLE) - find_library(IOKIT_LIBRARIES IOKit) - find_library(COREFOUNDATION_LIBRARIES CoreFoundation) -endif() - -if(UNIX AND NOT APPLE) - find_package(PkgConfig REQUIRED) - pkg_check_modules(LIBUDEV libudev) - if(${LIBUDEV_FOUND}) - add_definitions(-DHAVE_UDEV) - endif() -endif() - set(RESOURCES app.qrc) if(WIN32) @@ -99,10 +86,8 @@ set(SOURCES version_mismatch.cc ) -if(UNIX AND NOT APPLE) +if(UNIX) set(SOURCES ${SOURCES} serial_unix.cc) -elseif(APPLE) - set(SOURCES ${SOURCES} serial_mac.cc) elseif(WIN32) set(SOURCES ${SOURCES} serial_win.cc) endif() @@ -148,7 +133,7 @@ else() add_executable(${TARGET} ${SOURCES} ${HEADERS} ${RESOURCES}) endif() -set(LIBS ${QT_LIBRARIES} ${LIBUDEV_LIBRARIES} ${IOKIT_LIBRARIES} ${COREFOUNDATION_LIBRARIES}) +set(LIBS ${QT_LIBRARIES}) list(REMOVE_DUPLICATES LIBS) target_link_libraries(${TARGET} ${LIBS}) diff --git a/gui/app.pro b/gui/app.pro index 66e804d62..d7bbd11d0 100755 --- a/gui/app.pro +++ b/gui/app.pro @@ -12,6 +12,7 @@ ICON = images/appicon.icns QT += core \ gui \ network \ + serialport \ widgets \ xml @@ -27,15 +28,6 @@ unix:OBJECTS_DIR = objects unix:RCC_DIR = objects mac:DESTDIR = . -mac:LIBS += -framework IOKit -framework CoreFoundation -unix { - CONFIG += link_pkgconfig - packagesExist(libudev) { - DEFINES += HAVE_UDEV - PKGCONFIG += libudev - } -} - UI_DIR = tmp RESOURCES = app.qrc @@ -95,10 +87,8 @@ SOURCES += processwait.cc SOURCES += runmachine.cc SOURCES += upgrade.cc SOURCES += version_mismatch.cc -unix:!mac { +unix { SOURCES += serial_unix.cc -} else:mac { - SOURCES += serial_mac.cc } else:windows { SOURCES += serial_win.cc } diff --git a/gui/mainwindow.cc b/gui/mainwindow.cc index 63b1a2400..8ff9b058c 100644 --- a/gui/mainwindow.cc +++ b/gui/mainwindow.cc @@ -1091,7 +1091,9 @@ void MainWindow::dropEvent(QDropEvent* event) void MainWindow::setComboToDevice(QComboBox* comboBox, const QString& name) { for (int i=0; icount(); i++) { - if (comboBox->itemText(i) == name) { + QString value = comboBox->itemData(i).isValid()? + comboBox->itemData(i).toString() : comboBox->itemText(i); + if (value == name) { comboBox->setCurrentIndex(i); break; } @@ -1266,7 +1268,8 @@ void MainWindow::getWidgetValues() babelData_.inputType_ = BabelData::deviceType_; babelData_.inputDeviceFormat_ =formatList_[fidx].getName(); } - babelData_.inputDeviceName_ = ui_.inputDeviceNameCombo->currentText(); + babelData_.inputDeviceName_ = ui_.inputDeviceNameCombo->currentData().isValid()? + ui_.inputDeviceNameCombo->currentData().toString() : ui_.inputDeviceNameCombo->currentText(); comboIdx = ui_.outputFormatCombo->currentIndex(); fidx = ui_.outputFormatCombo->itemData(comboIdx).toInt(); @@ -1279,7 +1282,8 @@ void MainWindow::getWidgetValues() } else { babelData_.outputType_ = BabelData::noType_; } - babelData_.outputDeviceName_ = ui_.outputDeviceNameCombo->currentText(); + babelData_.outputDeviceName_ = ui_.outputDeviceNameCombo->currentData().isValid()? + ui_.outputDeviceNameCombo->currentData().toString() : ui_.outputDeviceNameCombo->currentText(); babelData_.xlateWayPts_ = ui_.xlateWayPtsCk->isChecked(); babelData_.xlateTracks_ = ui_.xlateTracksCk->isChecked(); diff --git a/gui/serial_unix.cc b/gui/serial_unix.cc index ce44710f5..05cbfd77a 100644 --- a/gui/serial_unix.cc +++ b/gui/serial_unix.cc @@ -18,97 +18,18 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, // USA. // -#include "mainwindow.h" -#ifdef HAVE_UDEV -#include // for udev_device_get_property_value, udev_device_get_devnode, udev_device_new_from_syspath, udev_device_unref, udev_enumerate_add_match_subsystem, udev_enumerate_get_list_entry, udev_enumerate_new, udev_enumerate_scan_devices, udev_enumerate_unref, udev_list_ent... +#include // for QComboBox +#include // for QList +#include // for QSerialPortInfo -#include // for QDebug -#include // for QSet -#include // for QString, operator== -#include // for QStringList -#include // for QComboBox +#include "mainwindow.h" // for MainWindow -#include "mainwindow.h" // for MainWindow - - -static QStringList dynamicDevices() -{ - struct udev* udev = udev_new(); - if (!udev) { - qDebug() << "Can't create udev"; - return QStringList(); - } - - QSet devices; - - struct udev_enumerate* enumerate = udev_enumerate_new(udev); - udev_enumerate_add_match_subsystem(enumerate, "tty"); - udev_enumerate_scan_devices(enumerate); - - struct udev_list_entry* device; - udev_list_entry_foreach(device, udev_enumerate_get_list_entry(enumerate)) { - const char* path = udev_list_entry_get_name(device); - struct udev_device* dev = udev_device_new_from_syspath(udev, path); - - bool okMaj; - bool okMin; - int major = QString(udev_device_get_property_value(dev, "MAJOR")).toInt(&okMaj); - int minor = QString(udev_device_get_property_value(dev, "MINOR")).toInt(&okMin); - if (!okMaj || !okMin) { - major = -1; - minor = -1; - } - - // see Documentation/devices.txt in the linux tree - if (!((major == 4 || major == 5) && 0 <= minor && minor <= 63)) { - devices << QString::fromUtf8(udev_device_get_devnode(dev)); - /* - udev_device_get_sysattr_list_entry(dev); - udev_device_get_tags_list_entry(dev); - struct udev_list_entry *prop; - qDebug() << "Device Node Path:" << udev_device_get_devnode(dev) << path; - udev_list_entry_foreach(prop, udev_device_get_properties_list_entry(dev)) { - qDebug() << " " << udev_list_entry_get_name(prop) - << "=>" << udev_list_entry_get_value(prop); - } - */ - } - udev_device_unref(dev); - } - udev_enumerate_unref(enumerate); - udev_unref(udev); - - QStringList list = devices.values(); - list.sort(); - return list; -} -#else -static QStringList dynamicDevices() -{ - return QStringList(); -} -#endif - - -static const char* deviceNames[] = { - "/dev/ttyS0", - "/dev/ttyS1", - "/dev/ttyS2", - "/dev/ttyS3", - "/dev/ttyUSB0", - "/dev/rfcomm0", - nullptr -}; void MainWindow::osLoadDeviceNameCombos(QComboBox* box) { - const QStringList devices = dynamicDevices(); - box->addItems(devices); - - for (int i=0; deviceNames[i] != nullptr; i++) { - if (!devices.contains(deviceNames[i])) { - box->addItem(deviceNames[i]); - } + const auto ports = QSerialPortInfo::availablePorts(); + for (const auto& info : ports) { + box->addItem(info.systemLocation()); } } diff --git a/gui/serial_win.cc b/gui/serial_win.cc index e4870775e..367428905 100644 --- a/gui/serial_win.cc +++ b/gui/serial_win.cc @@ -19,54 +19,21 @@ // USA. // -#include "mainwindow.h" +#include // for QComboBox +#include // for QList +#include // for QSerialPortInfo -#if 0 // Does not require Windows 2000 +#include "mainwindow.h" // for MainWindow -static const char* deviceNames[] = { - "com1:", - "com2:", - "com3:", - "com4:", - 0 -}; void MainWindow::osLoadDeviceNameCombos(QComboBox* box) { - for (int i=0; deviceNames[i]; i++) { - box->addItem(deviceNames[i]); - } -} - -#else // This code assumes Windows 2000 or later - -// Uses QueryDosDevice(), Minimum supported: Windows 2000 Professional/Server -#include -#include - -void MainWindow::osLoadDeviceNameCombos(QComboBox* box) -{ - char DevList[64*1024-1]; // a single byte more, and certain versions of windows - // always return QueryDosDevice()==0 && GetLastError()==ERROR_MORE_DATA. - // see http://support.microsoft.com/kb/931305 - // Get a list of all existing MS-DOS device names. Stores one or more ASCII strings followed by an extra null. - DWORD res = QueryDosDeviceA(NULL, DevList, sizeof(DevList)); - if (res == 0) { - DWORD err = GetLastError(); // could check for ERROR_INSUFFICIENT_BUFFER, and retry with a larger buffer. - // but DevList is already at the maximum size it can be without running into kb 931305. - // FIXME: This should be a QMessageBox::warning() - RJL - // fprintf(stderr,"QueryDosDevice() failed with %d. GetLastError()==%d.\n", res, err); - (void) err; - return; - } - - for (char* p=DevList; *p;) { - int len = strlen(p); - if (strncmp(p,"COM",3)==0) { - box->addItem((PCHAR)p); + const auto ports = QSerialPortInfo::availablePorts(); + for (const auto& info : ports) { + if (info.description().isEmpty()) { + box->addItem(info.portName()); + } else { + box->addItem(QString("%1 (%2)").arg(info.description(), info.portName()), info.portName()); } - p += len+1; // +1 to also skip the null character of each string } } - -#endif diff --git a/tools/Dockerfile_f32 b/tools/Dockerfile_f32 index e21bcad4f..b77f242f8 100644 --- a/tools/Dockerfile_f32 +++ b/tools/Dockerfile_f32 @@ -13,7 +13,7 @@ RUN dnf install --assumeyes git make valgrind diffutils which findutils langpack RUN dnf install --assumeyes libusbx-devel zlib-devel shapelib-devel && \ dnf clean all # Qt used by gpsbabel, gpsbabelfe -RUN dnf install --assumeyes qt5-qtbase-devel qt5-qtwebengine-devel qt5-linguist qt5-qttranslations && \ +RUN dnf install --assumeyes qt5-qtbase-devel qt5-qtserialport-devel qt5-qtwebengine-devel qt5-linguist qt5-qttranslations && \ dnf clean all # tools to build the docs RUN dnf install --assumeyes expat desktop-file-utils libxslt docbook-style-xsl fop && \ diff --git a/tools/Dockerfile_f33 b/tools/Dockerfile_f33 index d2637df73..75cf4f945 100644 --- a/tools/Dockerfile_f33 +++ b/tools/Dockerfile_f33 @@ -13,7 +13,7 @@ RUN dnf install --assumeyes git make valgrind diffutils which findutils langpack RUN dnf install --assumeyes libusbx-devel zlib-devel shapelib-devel && \ dnf clean all # Qt used by gpsbabel, gpsbabelfe -RUN dnf install --assumeyes qt5-qtbase-devel qt5-qtwebengine-devel qt5-linguist qt5-qttranslations && \ +RUN dnf install --assumeyes qt5-qtbase-devel qt5-qtserialport-devel qt5-qtwebengine-devel qt5-linguist qt5-qttranslations && \ dnf clean all # tools to build the docs RUN dnf install --assumeyes expat desktop-file-utils libxslt docbook-style-xsl fop && \ diff --git a/tools/Dockerfile_f34 b/tools/Dockerfile_f34 index 69c1675d0..54350eec6 100644 --- a/tools/Dockerfile_f34 +++ b/tools/Dockerfile_f34 @@ -13,7 +13,7 @@ RUN dnf install --assumeyes git make valgrind diffutils which findutils langpack RUN dnf install --assumeyes libusbx-devel zlib-devel shapelib-devel && \ dnf clean all # Qt used by gpsbabel, gpsbabelfe -RUN dnf install --assumeyes qt5-qtbase-devel qt5-qtwebengine-devel qt5-linguist qt5-qttranslations && \ +RUN dnf install --assumeyes qt5-qtbase-devel qt5-qtserialport-devel qt5-qtwebengine-devel qt5-linguist qt5-qttranslations && \ dnf clean all # tools to build the docs RUN dnf install --assumeyes expat desktop-file-utils libxslt docbook-style-xsl fop && \ diff --git a/tools/Dockerfile_focal b/tools/Dockerfile_focal index 85d618743..b171d3d9b 100644 --- a/tools/Dockerfile_focal +++ b/tools/Dockerfile_focal @@ -57,6 +57,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ qttools5-dev-tools \ qttranslations5-l10n \ qtwebengine5-dev \ + libqt5serialport5-dev \ && rm -rf /var/lib/apt/lists/* # pkgs needed to generate coverage report: -- 2.30.2